package njci.softerware.com.myexpress2017.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import njci.softerware.com.myexpress2017.R;

/**
 * Created by 徐连宝 on 2016/9/21.
 */
public class MyListAdapter extends BaseAdapter {

    Context context;
    List<Package> list;

    public MyListAdapter(Context context, List<Package> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list == null ? 0 : list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return list == null ? 0 : position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        Package p = (Package) getItem(position);
        try {
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.lsit_item, null);
                viewHolder.date_item = (TextView) convertView.findViewById(R.id.date_item);
                viewHolder.item_count = (TextView) convertView.findViewById(R.id.item_count);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            setStatus(p, viewHolder);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }

    private void setStatus(Package p, ViewHolder viewHolder) {
        viewHolder.date_item.setText(p.getDate());
        viewHolder.item_count.setText("共计 | "+p.getCount()+"件");
    }

    public static class ViewHolder {
        TextView date_item;
        TextView item_count;
    }
}
